package com.qst.web;

import com.qst.po.JsonData;

import com.qst.po.PageData;
import com.qst.po.Student;
import com.qst.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class StudentController {
    @Autowired
    private IStudentService studentService;
//   学生登陆
    @RequestMapping("/tologin")
    public String tologin(){
        return "login";
    }

    @RequestMapping("login")
    @ResponseBody
    public JsonData login(String name, String password, HttpSession session){
        JsonData data =new JsonData();
        Student stu =studentService.login(name,password);
        if(stu!=null){
            data.setCode(200);
//            放在 缓存
            session.setAttribute("loginstu",stu);
        }else{
            data.setCode(500);
            data.setMsg("登录失败,学生名或者密码错误");
        }
        return data;
    }
//回到主页
    @RequestMapping("/welcome")
    public String welcome(){
        return "welcome";
    }
    //    slists
    @RequestMapping("/slists")
    public String slists(){
        return "/student/studentlist";
    }
//分页
    @RequestMapping("/studentlistbypage")
    @ResponseBody
    public PageData<Student> studentlistbypage(int page, int limit ){
        PageData<Student> StudentPageData =new PageData<Student>();
//    获取conut总数 dat分页数据
        int stutoallcount=studentService.getstutoallcount();
        List<Student> stupage =studentService.getpage(page,limit);

        StudentPageData.setCode(0);
        StudentPageData.setMsg(" ");
        StudentPageData.setCount(stutoallcount);
        StudentPageData.setData(stupage);
        return StudentPageData;
    }
//tj
@RequestMapping("/toaddstudent")
public String toaddstu(){
    return "/student/addstudent";
}

@RequestMapping("/addstudent")
@ResponseBody
public JsonData addstudent(Student student){
    JsonData data = new JsonData();
    int count = studentService.addstudent(student);
    if(count>0){
        data.setCode(200);
    }else{
        data.setCode(500);
        data.setMsg("添加失败，请重新添加");
    }
    return data;
}

///toupdatestudent
@RequestMapping("/toupdatestudent")
public String toupdatestudent(int id, HttpServletRequest request){
    Student student =studentService.getstudentbyid(id);
    request.setAttribute("updatestudent",student);
    return "student/updatestudent";
}

    @RequestMapping("/updatestudent")
    @ResponseBody
    public JsonData updatestudent(Student student){
        JsonData data = new JsonData();
        int count = studentService.updatestudent(student);
        if(count>0){
            data.setCode(200);
        }else {data.setCode(500);
            data.setMsg("修改失败，请重新修改");}
        return data;
    }

    @RequestMapping("/student/deletestudent")
    @ResponseBody
    public JsonData deletestudent(int id){
        JsonData data =new JsonData();
        int count =studentService.deletestudent(id);
        if(count>0){
            data.setCode(200);
        }else{
            data.setCode(500);
            data.setMsg("删除失败，请重新删除");
        }
        return data;
    }

}
